// serve
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
// var supervisor = require('supervisor');

// 引用bodyParser
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({
  extended: true
})); // for parsing application/x-www-form-urlencoded

// 设置跨域请求
app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Credentials', true);
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
  res.header('X-Powered-By', ' 3.2.1');
  res.header('Content-Type', 'application/json;charset=utf-8');
  next();
});

// redis
var Redis = require('ioredis');
var redis = new Redis({
  port: 6379, // Redis port
  host: '127.0.0.1', // Redis host
  family: 4, // 4 (IPv4) or 6 (IPv6)
  password: '',
  db: 0
});

var users;
redis.lrange('users', 0, -1).then(result => {
  console.log(result);
  users = result;
});

// 写个接口
app.get('/api/user', function (req, res) {
  res.status(200);
  res.json(users);
});

// 配置服务端口
const port = 8888;
app.listen(port, () => {
  console.log('Express server listening on port ' + port);
});
